package LeetCode.month02.day55;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

public class No637 {
    public List<Double> averageOfLevels(TreeNode root) {
        List<Double> res = new ArrayList<>();
        if(root==null) return res;
        Deque<TreeNode> deque = new ArrayDeque<>();
        deque.add(root);
        while(!deque.isEmpty()){
            int n = deque.size();
            double sum = 0;
            for (int i = 0; i < n; i++) {
                TreeNode curr = deque.poll();
                sum+=curr.val;
                if(curr.left!=null) deque.add(curr.left);
                if(curr.right!=null) deque.add(curr.right);
            }
            res.add(sum/n);
        }
        return res;
    }
}
